# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=cargo-c
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.13
pkgrel=1
pkgdesc='A cargo subcommand to build and install C-ABI compatibile dynamic and static libraries (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/lu-zero/cargo-c/'
msys2_references=(
  'archlinux: cargo-c'
  'purl: pkg:cargo/cargo-c'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libgit2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl")
options=('!strip')
source=("https://github.com/lu-zero/cargo-c/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-${pkgver}.Cargo.lock"::"https://github.com/lu-zero/cargo-c/releases/download/v${pkgver}/Cargo.lock")
sha256sums=('57dffba592179c7ca2b0322d28265b6962750eebb3a23b28ad677371cc10c36e'
            'e5548cf627d93b53c6f32ccd21315e2fd64d8a08990100426a40c41d2ff832c0')

prepare() {
  cp "${_realname}-${pkgver}.Cargo.lock" "${_realname}-${pkgver}/Cargo.lock"
  cd "${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

_env() {
  export LIBGIT2_NO_VENDOR=1
  export OPENSSL_NO_VENDOR=1
  export LIBSSH2_SYS_USE_PKG_CONFIG=1
  export LIBSQLITE3_SYS_USE_PKG_CONFIG=1
}

build() {
  cd "${_realname}-${pkgver}"

  _env
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build --frozen --profile release-strip
}

check() {
  cd "${_realname}-${pkgver}"

  _env
  cargo test --frozen --profile release-strip
}

package() {
  cd "${_realname}-${pkgver}"

  _env
  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}" \
    --profile release-strip

  install -Dm644 "LICENSE" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
